package com.groupbuying.payconsumer.mq;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * 支付中心消息消费MQ配置文件
 * @Author Vincentxin
 * @Date 2019-05-10
 */
@Configuration
public class PayMQConsumer {

    @Autowired
    private PayResultListener payResultListener;

    public static final Logger LOGGER = LoggerFactory.getLogger(PayMQConsumer.class);


    public PayMQConsumer() {
    }

    @Bean
    public Consumer getPayMQConsumer() {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.GROUP_ID, PayMQConfig.GROUP_ID);
        properties.put(PropertyKeyConst.AccessKey, PayMQConfig.ACCESS_KEY);
        properties.put(PropertyKeyConst.SecretKey, PayMQConfig.SECRET_KEY);
        properties.put(PropertyKeyConst.NAMESRV_ADDR, PayMQConfig.NAMESRV_ADDR);
        Consumer consumer = ONSFactory.createConsumer(properties);
        try {
            //订阅支付中心下所有支付回执的Tag
            consumer.subscribe(PayMQConfig.TOPIC, PayMQConfig.TAG_ALL ,payResultListener);
            consumer.start();
            LOGGER.info("producer is start !!!  groupId:{" + PayMQConfig.GROUP_ID+ "}");
        } catch (Exception var4) {
            LOGGER.error(String.format("producer is error  groupId:{" + PayMQConfig.GROUP_ID + "}", var4.getMessage(), var4));
        }

        return consumer;
    }
}
